package com.sun.btrace.cmd;

import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import com.sun.btrace.client.Admin;

public abstract class CommandCase {
	protected Admin context ;
	protected   final Console con=System.console();
	protected  PrintWriter out;
	protected  BufferedReader in; 
	
	private boolean logFile ;

	public CommandCase(Admin context){
		this.context = context ;
		logFile = false ;
	}
	
	public CommandCase(){
		
	}
	public void initOutPrint(String... args) throws Exception{
		int count =0 ;
		for (count = 1; count < args.length; ++count) {
			if( args[count] .equals(">")){
				this.out = new PrintWriter(new FileWriter(new File(args[++count]),false));
				logFile = true ;
				break ;
			}else if( args[count] .equals(">>")){
				this.out = new PrintWriter(new FileWriter(new File(args[++count]),true));
				logFile = true ;
				break ;
			}
		}
	}
	public void setContext(Admin context){
		this.context = context ;
		this.out = context.getOut();
		this.in = context.getIn() ;
	}
	public abstract void realCMDAction(String...args) ;
	public abstract void help() ;
	public void  cmd(String... args){
		try {
			initOutPrint(args) ;
			realCMDAction(args);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace(out);
		}finally{
			if(this.logFile)
				this.out.close() ;
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
